package com.onenine.app.view;

import android.content.Context;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import com.onenine.app.R;
import com.onenine.app.utils.CHelper;
import com.onenine.app.utils.RoundViewOutlineProvider;
import com.onenine.common.util.SizeUtils;
import com.onenine.glide.GlideApp;

import java.util.LinkedHashMap;

import cn.jzvd.JZDataSource;
import cn.jzvd.Jzvd;
import cn.jzvd.JzvdStd;

/**
 * 循环播放的视频播放
 */
public class VideoPlayLayout extends FrameLayout implements View.OnClickListener {

    private JzvdStd mJzvdStd;
    private View mBtnDelete;
    private String mVideoUrl;
    private String mFirstFramePath;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public VideoPlayLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context)
                .inflate(R.layout.layout_video_view, this, true);
        setClipToPadding(false);
        int padding = SizeUtils.dp2px(8);
        setPadding(padding, padding, padding, padding);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mJzvdStd = findViewById(R.id.vv_view);
        mBtnDelete = findViewById(R.id.btn_del);
        mJzvdStd.setOutlineProvider(new RoundViewOutlineProvider(getResources()
                .getDimensionPixelOffset(R.dimen.common_round_corner)));
        mJzvdStd.setClipToOutline(true);
        mBtnDelete.setOnClickListener(this);
    }

    public void playVideo(String firstFramePath, final String videoUrl) {
        mVideoUrl = videoUrl;
        mFirstFramePath = firstFramePath;
        int videoWidth = getResources().getDimensionPixelOffset(R.dimen.small_video_width);
        int videoHeight = getResources().getDimensionPixelOffset(R.dimen.small_video_height);
        GlideApp.with(getContext()).load(CHelper.getOssThumbImageUrl(firstFramePath, videoWidth))
                .override(videoWidth, videoHeight).into(mJzvdStd.thumbImageView);
        mJzvdStd.setUp(getJZDataSource(videoUrl), Jzvd.SCREEN_WINDOW_LIST);
    }

    public JZDataSource getJZDataSource(String videoUrl) {
        LinkedHashMap<String, String> dataMap = new LinkedHashMap<>();
        dataMap.put(JZDataSource.URL_KEY_DEFAULT, videoUrl);
        JZDataSource dataSource = new JZDataSource(dataMap);
        dataSource.looping = true;
        dataSource.currentUrlIndex = 0;
        dataSource.title = "";
        return dataSource;
    }

    public String getVideoUrl() {
        return mVideoUrl == null ? "" : mVideoUrl;
    }

    public String getFirstFramePath() {
        return mFirstFramePath == null ? "" : mFirstFramePath;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_del) {
            setVisibility(View.GONE);
        }
    }

    @Override
    protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
        if (changedView == this && visibility != View.VISIBLE) {
            mVideoUrl = null;
            mFirstFramePath = null;
        }
    }

}
